package Test;


public class TTT10 {
    private static int state = 0; // 需要是共享的静态变量

    public static void main(String[] args) {
        Object lock = new Object();

        Thread a = new Thread(new MyRunnable("a", 0, lock));
        Thread b = new Thread(new MyRunnable("b", 1, lock));
        Thread c = new Thread(new MyRunnable("c", 2, lock));

        a.start();
        b.start();
        c.start();
    }

    static class MyRunnable implements Runnable {
        String printContent;
        int targetState;  // 改个更清晰的名字
        Object lock;

        public MyRunnable(String printContent, int targetState, Object lock) {
            this.printContent = printContent;
            this.targetState = targetState;
            this.lock = lock;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                synchronized (lock) {
                    // 使用共享的state变量来判断
                    while (state % 3 != targetState) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }
                    System.out.print(printContent);
                    state++;  // 更新共享状态
                    lock.notifyAll();
                }
            }
        }
    }
}
